package org.yxj.storagefilemobile;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import androidx.core.app.ActivityCompat;
import org.yxj.lib_storage.LogUtils;

/**
 * 获取运营商信息
 *
 * @author yxj on 2024/7/10
 */
public class GetPhoneInfoUtil {

    private TelephonyManager telephonyManager;

    public GetPhoneInfoUtil(Context context) {
        boolean b = context.getPackageManager().hasSystemFeature(Context.TELEPHONY_SERVICE);;

        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_NUMBERS)
                != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }


        String Number = telephonyManager.getLine1Number();
        LogUtils.LOG_V("[GetPhoneInfoUtil]->" +
                new StringBuilder()
                        .append("b=" + b).append("\n")
                        .append("Number=" + Number).append("\n")
                        .append("CallState=" + telephonyManager.getCallState()).append("\n")
                        .append("PhoneType=" + telephonyManager.getPhoneType()).append("\n")
//                        .append("IMSI=" + getIMSI()).append("\n")
                        .append("ProvidersName=" + getProvidersName()).append("\n")
                        .append("SIM运营商代码=" + simOperator()).append("\n")
                        .append("SIM运营商=" + simOperatorName()).append("\n")
                        .append("网络运营商代码=" + simNetworkOperator()).append("\n")
                        .append("网络运营商=" + networkOperatorName()).append("\n")
                        .toString());
    }


    /**
     * 电话类型
     *
     * @return
     */
    public String getProvidersName() {
        String ProvidersName = null;
        String IMSI = simOperator();
        // IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信
        try {
            if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
                ProvidersName = "中国移动";
            } else if (IMSI.startsWith("46001")) {
                ProvidersName = "中国联通";
            } else if (IMSI.startsWith("46003")) {
                ProvidersName = "中国电信";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ProvidersName;
    }


    //使用这个需要在manifest文件里面添加 权限<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    //或者：IMSI=telephonyManager.getSimOperator();
    public String getIMSI() {
        return telephonyManager.getSubscriberId();
    }

    /**
     * SIM运营商代码
     *
     * @return
     */
    public String simOperator() {
        return telephonyManager.getSimOperator();
    }


    /**
     * SIM运营商
     *
     * @return
     */
    public String simOperatorName() {
        return telephonyManager.getSimOperatorName();
    }


    /**
     * 网络运营商代码
     *
     * @return
     */
    public String simNetworkOperator() {
        return telephonyManager.getNetworkOperator();
    }




    /**
     * 网络运营商
     */
    public String networkOperatorName() {
        return telephonyManager.getNetworkOperatorName();
    }
}
